<?php

namespace common\model;

use think\model\RelationModel;

class MessageModel extends RelationModel
{
    const NOTIFY_TYPE_TASK_REMIND = 'task_remind';
    const NOTIFY_TYPE_APPROVAL = 'approval';
    const NOTIFY_TYPE_EXAMINE = 'examine';
    const NOTIFY_TYPE_TODO = 'todo';
    const NOTIFY_TYPE_MESSAGE_DEMAND = 'message_demand';
    const NOTIFY_TYPE_MESSAGE_TASK = 'message_task';
    const NOTIFY_TYPE_MESSAGE_REVIEW = 'message_review';
    const NOTIFY_TYPE_MESSAGE_QA = 'message_qa';
    const NOTIFY_TYPE_MESSAGE_PRODUCER = 'message_producer';
    const NOTIFY_TYPE_MESSAGE_CAPACITY = 'message_capacity';
    const NOTIFY_TYPE_MESSAGE_ORDER = 'message_order';
    const EVERYDAY_REPORT = 'everyday_report';

    const CONTENT_TYPE_TEXT = 'text';
    const CONTENT_TYPE_IMAGE = 'image';
    const CONTENT_TYPE_SYSTEM_HINT = 'system_hint';
    const CONTENT_TYPE_CARD_MESSAGE_V2 = 'card_message_v2';
    const CONTENT_TYPE_CARD_MESSAGE = 'card_message';
    const CONTENT_TYPE_FORWARD = 'forward';
    const CONTENT_TYPE_REPLY = 'reply';
    const CONTENT_TYPE_LINK = 'link';
    const CONTENT_TYPE_FILE = 'file';
    const CONTENT_TYPE_COMMON_DOT = 'common_dot';
    const CONTENT_TYPE_COMMON_POPOVER = 'common_popover';
    const CONTENT_TYPE_COMMON_MODAL = 'common_modal';

    //自动验证
    protected $_validate = [
        ['chat_global_id', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['sender', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['project_id', '', '', self::EXISTS_VALIDATE, 'integer', self::MODEL_INSERT],//必须字段
        ['type', ['normal', 'broadcast'], '', self::EXISTS_VALIDATE, 'in'],
        ['content', '', '', self::EXISTS_VALIDATE, 'array'],
        ['channel_id', [0, 1], '', self::EXISTS_VALIDATE, 'in'],
        ['content_type', ['text',
            'common_dot', 'common_popover', 'common_modal', // 新版消息推送
            'notice', 'file', 'audio', 'image', 'card_message', 'system_hint', 'link', 'reply', 'rich_text', 'urge', 'forward', 'card_message_v2' // 旧版消息推送
        ], '', self::EXISTS_VALIDATE, 'in'],
        ['is_popup', ['yes', 'no'], '', self::EXISTS_VALIDATE, 'in'],
        ['notify_type', ['default',
            'message_demand', 'message_task', 'message_qa', 'message_producer', 'message_capacity','message_review', 'message_order', // 新版消息推送
            'task_remind', 'approval', 'examine', 'todo', 'everyday_report' // 旧版消息推送
        ], '', self::EXISTS_VALIDATE, 'in'],
    ];

    // 自动完成
    protected $_auto = [
        ['content', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['global_id', 'create_uuid', self::MUST_VALIDATE, 'function', self::MODEL_INSERT],
        ['sent_at', 'time', self::MODEL_INSERT, 'function'],

    ];

    /**
     * 获取器：消息内容
     * @param $value
     * @return array|mixed
     */
    public function getContentAttr($value)
    {
        if (!empty($value)) {
            return json_decode($value, true);
        }
        return [];
    }
}
